/** 
 * File-Name:FundAction.java
 *
 * Created on 2011-6-2 下午10:30:04
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package xiaoyou.web;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import xiaoyou.entity.Fund;
import xiaoyou.entity.User;
import xiaoyou.neoutil.CommonAction;
import xiaoyou.neoutil.CommonService;
import xiaoyou.neoutil.FileUtils;
import xiaoyou.neoutil.Page;
import xiaoyou.service.ArticleRepository;
import xiaoyou.service.FundRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午10:30:04
 */
public class FundAction extends CommonAction {

	private static final long serialVersionUID = -2032909060568018065L;
	private static Logger log = Logger.getLogger(FundAction.class);
	private Fund fund;
	private File donationNumFile;
	private String donationNumFileFileName;
	private String donationNumFileContentType;
	private String fileSavePath;
	private Page<Fund> page = new Page<Fund>(10);

	public String addFund() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				fund.setAuthor(user);

				if (donationNumFile != null) {
					String fileName = fund.getUuid()
							+ FileUtils
									.getExtention(getDonationNumFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getDonationNumFile());

					fund.setDonationNumFile(fileName);
				}

				if (CommonService.SUCCESS == getFundRepository().save(fund)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}
	public String downloadFundEnclosure() {
		return SUCCESS;
	}
	
	public InputStream getEnclosureFile() {
		try {
			String uuid = getRequestParameter("uuid");
			fund = getFundRepository().get(uuid);

			System.err
					.println(getFileSavePath() + "\\" + fund.getDonationNumFile());

			return new java.io.FileInputStream(getFileSavePath() + "\\"
					+ fund.getDonationNumFile());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public String toEditFund() {
		try {
			String uuid = getRequestParameter("uuid");
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else if (uuid == null || "".equals(uuid)) {
				return INPUT;
			} else {
				fund = getFundRepository().get(uuid);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String editFund() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				
				if (donationNumFile != null) {
					String fileName = fund.getUuid()
							+ FileUtils.getExtention(getDonationNumFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getDonationNumFile());

					fund.setDonationNumFile(fileName);
				}
				
				if (CommonService.SUCCESS == getFundRepository().update(fund)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String deleteFund() {
		try {
			User user = (User) getSessionObject("loginUser");
			long fundId = Long.parseLong(getRequestParameter("id"));
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				if (CommonService.SUCCESS == getFundRepository().delete(fundId)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getFundByUuid() {
		try {
			String uuid = getRequestParameter("uuid");
			if (uuid == null || "".equals(uuid)) {
				return INPUT;
			} else {
				fund = getFundRepository().get(uuid);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getAllFunds() {
		try {
			page = getFundRepository().getFunds(page);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public Fund getFund() {
		return fund;
	}

	public void setFund(Fund fund) {
		this.fund = fund;
	}

	public Page<Fund> getPage() {
		return page;
	}

	public void setPage(Page<Fund> page) {
		this.page = page;
	}

	public File getDonationNumFile() {
		return donationNumFile;
	}

	public void setDonationNumFile(File donationNumFile) {
		this.donationNumFile = donationNumFile;
	}

	public void setFileSavePath(String fileSavePath) {
		this.fileSavePath = fileSavePath;
	}

	public String getFileSavePath() {
		return ServletActionContext.getRequest().getRealPath(fileSavePath);
	}

	public String getDonationNumFileContentType() {
		return donationNumFileContentType;
	}

	public void setDonationNumFileContentType(String donationNumFileContentType) {
		this.donationNumFileContentType = donationNumFileContentType;
	}

	public String getDonationNumFileFileName() {
		return donationNumFileFileName;
	}

	public void setDonationNumFileFileName(String donationNumFileFileName) {
		this.donationNumFileFileName = donationNumFileFileName;
	}
}
